package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.Constants;
import com.sofwin.common.DataStatus;
import com.sofwin.common.ResponseData;
import com.sofwin.pojo.SysDept;
import com.sofwin.pojo.SysRole;
import com.sofwin.pojo.SysUser;
import com.sofwin.pojo.vo.UserVO;
import com.sofwin.system.DeptService;
import com.sofwin.system.RoleService;
import com.sofwin.system.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @packageName: com.sofwin.controller
 * @user: andyliu
 * @date: 2021/9/22 11:27
 * @email 115176513@qq.com
 * @description: TODO
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Reference
    public UserService userService;

    @Reference
    public DeptService deptService;

    @Reference
    public RoleService roleService;

    /**
     * 跳转到列表页面
     * @return
     */
    @GetMapping("/toData")
    public String toData(){
        return "user/data";
    }

    /**
     * 异步加载分页数据
     * @param user
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @PostMapping("/data")
    @ResponseBody
    public ResponseData data(UserVO user, @RequestParam(required = true,defaultValue = "1") Integer pageNumber, @RequestParam(required = true,defaultValue = Constants.DEFAULT_PAGESIZE) Integer pageSize){
        PageInfo<SysUser> pageInfo = userService.queryUser(user, pageNumber, pageSize);
        return new ResponseData(200,pageInfo);
    }

    /**
     * 跳转到编辑或新增页面
     * @param user
     * @param model
     * @return
     */
    @GetMapping("/toEdit")
    public String toEdit(SysUser user, Model model){
        if(user!=null && user.getUserId()!=null){
            user = userService.queryUserById(user.getUserId());
            model.addAttribute("user",user);
        }
        SysDept dept = new SysDept();
        dept.setStatus(DataStatus.ENABLE.getValue());
        List<SysDept> depts = deptService.queryDepts(dept);
        model.addAttribute("depts",depts);
        SysRole role = new SysRole();
        role.setStatus(0);
        List<SysRole> roles = roleService.queryRole(role);
        model.addAttribute("roles",roles);

        return "user/edit";
    }

    /**
     * 新增或更新用户信息
     * @param user
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ResponseData save(SysUser user){
        boolean status = userService.saveUser(user);
        if(status){
            return ResponseData.success("保存成功");
        }
        return new ResponseData(400,"操作失败");
    }
    @DeleteMapping("/deletes")
    @ResponseBody
    public ResponseData delete(Integer[] ids){
        boolean status = userService.deleteUserByIds(ids);
        if(status) return ResponseData.success("删除成功！");
        return ResponseData.error();
    }

    @PostMapping("/checkLoginName")
    @ResponseBody
    public boolean checkLoginName(SysUser user){
        SysUser user1 = userService.queryUserByUserName(user.getLoginName());
        if(user1==null) return true;
        return false;
    }

}
